const AuthUtil = require('../utils/AuthUtil');
const StringUtil = require('../utils/StringUtil');
const UserService = require('../service/UserService');
const { UserVerifiedStatus } = require('../constants/DbEnum');

/**
 * get user balance
 * @param{event} event
 * @param{event.body} event.body
 * @param{event.body.username} event.body.username
 * @param{context} context
 * @return {Object}
 */
async function getBalance(event, context) {
  let user = { username: AuthUtil.getUsername(event) };
  let balance = await UserService.getBalance(user);
  return { balance: balance };
}

/**
 * submitUserInfo
 * @param{event} event
 * @param{event.body} event.body
 * @param{event.body.realName} event.body.realName
 * @param{event.body.passportNo} event.body.passportNo
 * @param{event.body.birthDate} event.body.birthDate
 * @param{event.body.country} event.body.country
 * @param{event.body.province} event.body.province
 * @param{event.body.city} event.body.city
 * @param{event.body.area} event.body.area
 * @param{event.body.address} event.body.address
 * @param{event.body.postcode} event.body.postcode
 * @param{context} context
 * @return {Object}
 */
async function submitUserInfo(event, context) {
  let authInfo = AuthUtil.getUserInfoFromAuth(event);
  let updatedUser = authInfo;
  updatedUser.realName = event.body.realName;
  updatedUser.passportNo = event.body.passportNo;
  updatedUser.birthDate = event.body.birthDate;
  updatedUser.country = event.body.country;
  updatedUser.province = event.body.province;
  updatedUser.city = event.body.city;
  updatedUser.area = event.body.area;
  updatedUser.address = event.body.address;
  updatedUser.postcode = event.body.postcode;
  if (!authInfo.phone) {
    updatedUser.phone = event.body.phone;
  }
  await UserService.submitUserInfo(updatedUser);

  return {};
}

/**
 * submitPhoto
 * @param{event} event
 * @param{event.body} event.body
 * @param{event.body.type} event.body.type
 * @param{event.body.photo} event.body.photo
 * @param{context} context
 * @return {Object}
 */
async function submitPhoto(event, context) {
  let user = { username: AuthUtil.getUsername(event) };
  let photo = event.body.photo;
  let type = event.body.type;
  await UserService.submitPhoto(user, photo, type);

  return {};
}

/**
 * submitPhoto
 * @param{event} event
 * @param{event.body} event.body
 * @param{context} context
 * @return {Object}
 */
async function getCurrentUserInfo(event, context) {
  let user = { username: AuthUtil.getUsername(event) };
  console.info('CurrentUserInfo', user.username);
  let existedUser = await UserService.findOne(user);

  return {
    personalInfo_submitted: existedUser ? true : false,
    personalInfo_verified: existedUser
      ? existedUser.personalInfoVerified
      : UserVerifiedStatus.un_verified,
    realName: existedUser ? existedUser.realName : '',
    passportNo: existedUser ? StringUtil.replaceStarPassport(existedUser.passportNo) : '',
    country: existedUser ? existedUser.country : '',
    passportPhoto_uploaded: existedUser ? (existedUser.passportPhoto ? true : false) : false,
    passportPhoto_verified: existedUser
      ? existedUser.passportPhotoVerified
      : UserVerifiedStatus.un_verified,
    billPhoto_uploaded: existedUser ? (existedUser.billPhoto ? true : false) : false,
    billPhoto_verified: existedUser
      ? existedUser.billPhotoVerified
      : UserVerifiedStatus.un_verified,
  };
}

/**
 * create
 * @param {event} event
 * @param {String} event.body.username
 * @param {String} event.body.email
 * @param {Decimal} event.body.balance
 * @param {context} context
 * @return {Object}
 */
async function create(event, context) {
  const { username, email, balance } = event.body;

  await UserService.create({ username, email, balance });

  return {};
}

/**
 * depositCash
 * @param {Object} event
 * @param {Object} context
 * @return {Decimal}
 */
async function depositCash(event, context) {
  const username = AuthUtil.getUsername(event);
  const { amount } = event.body;

  await UserService.depositCash(username, Number(amount));

  return amount;
}

/**
 * withdrawCash
 * @param {Object} event
 * @param {Object} context
 * @return {Decimal}
 */
async function withdrawCash(event, context) {
  const username = AuthUtil.getUsername(event);
  const { amount } = event.body;

  await UserService.withdrawCash(username, Number(amount));

  return amount;
}

module.exports = {
  getBalance,
  create,
  submitUserInfo,
  submitPhoto,
  getCurrentUserInfo,
  depositCash,
  withdrawCash,
};
